package com.ruoyi.statisticalCenter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.InitTableVo;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.statisticalCenter.domain.ScOutPortWarehouseGrossProfitSheet;
import com.ruoyi.statisticalCenter.domain.vo.ScOutPortWareDispositionVo;
import com.ruoyi.statisticalCenter.domain.vo.ScOutPortWarehouseGrossProfitSheetVo;
import com.ruoyi.statisticalCenter.mapper.ScOutPortWarehouseGrossProfitSheetMapper;
import com.ruoyi.statisticalCenter.service.ScOutPortWarehouseGrossProfitSheetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ScOutPortWarehouseGrossProfitSheetServiceImpl implements ScOutPortWarehouseGrossProfitSheetService {
    @Autowired
    private ScOutPortWarehouseGrossProfitSheetMapper scOutPortWarehouseGrossProfitSheetMapper;

    @Autowired
    private TableService tableService;

    @Override
    public R collect(ScOutPortWarehouseGrossProfitSheetVo vo) {
        //获取配置参数
        ScOutPortWareDispositionVo scOutPortWareDispositionVo = scOutPortWarehouseGrossProfitSheetMapper.selectDisposition();
        BeanUtils.copyProperties(scOutPortWareDispositionVo, vo);
        //获取全部中转费
        String sum = scOutPortWarehouseGrossProfitSheetMapper.collectSum(vo);
        vo.setSum(sum);
        //获取中转费信息
        PageHelper.startPage(vo.getPageDate());
        List<ScOutPortWarehouseGrossProfitSheet> sheets = scOutPortWarehouseGrossProfitSheetMapper.collectTransfer(vo);
        //获取包仓内信息
        List<ScOutPortWarehouseGrossProfitSheet> scOutPortWarehouseGrossProfitSheets = scOutPortWarehouseGrossProfitSheetMapper.collectIW(vo);
        //获取包仓外信息
        List<ScOutPortWarehouseGrossProfitSheet> owList = scOutPortWarehouseGrossProfitSheetMapper.collectOW(vo);
        //获取6kg以上信息
//        List<ScOutPortWarehouseGrossProfitSheet> overList = scOutPortWarehouseGrossProfitSheetMapper.collectOverSix(vo);
        //获取偏远信息
        List<ScOutPortWarehouseGrossProfitSheet> remote = scOutPortWarehouseGrossProfitSheetMapper.collectRemote(vo);
        //获取偏远信息
//        List<ScOutPortWarehouseGrossProfitSheet> ln = scOutPortWarehouseGrossProfitSheetMapper.collectLN(vo);

        for (ScOutPortWarehouseGrossProfitSheet sheet : sheets) {
            for (ScOutPortWarehouseGrossProfitSheet scOutPortWarehouseGrossProfitSheet : scOutPortWarehouseGrossProfitSheets) {
                if (sheet.getName().equals(scOutPortWarehouseGrossProfitSheet.getName())) {
                    sheet.setIwWeight(scOutPortWarehouseGrossProfitSheet.getIwWeight());
                    sheet.setIwStopLossWeight(scOutPortWarehouseGrossProfitSheet.getIwStopLossWeight());
                    sheet.setDailyAssessmentWeight(scOutPortWarehouseGrossProfitSheet.getDailyAssessmentWeight());
                    sheet.setIwDifference(scOutPortWarehouseGrossProfitSheet.getIwDifference());
                    sheet.setIwFee(scOutPortWarehouseGrossProfitSheet.getIwFee());
                    sheet.setIwParcelCount(scOutPortWarehouseGrossProfitSheet.getIwParcelCount());
                    sheet.setIwAverageWeight(scOutPortWarehouseGrossProfitSheet.getIwAverageWeight());
                    sheet.setIwAssessmentAverageWeight(scOutPortWarehouseGrossProfitSheet.getIwAssessmentAverageWeight());
                    sheet.setIwPerParcelFee(scOutPortWarehouseGrossProfitSheet.getIwPerParcelFee());
                    sheet.setIwPerKgFee(scOutPortWarehouseGrossProfitSheet.getIwPerKgFee());
                }
            }
            for (ScOutPortWarehouseGrossProfitSheet scOutPortWarehouseGrossProfitSheet : owList) {
                if (sheet.getName().equals(scOutPortWarehouseGrossProfitSheet.getName())) {
                    sheet.setOwWeight(scOutPortWarehouseGrossProfitSheet.getOwWeight());
                    sheet.setOwFee(scOutPortWarehouseGrossProfitSheet.getOwFee());
                    sheet.setOwParcelCount(scOutPortWarehouseGrossProfitSheet.getOwParcelCount());
                    sheet.setOwPerParcelFee(scOutPortWarehouseGrossProfitSheet.getOwPerParcelFee());
                    sheet.setOwPerKgFee(scOutPortWarehouseGrossProfitSheet.getOwPerKgFee());
                }
            }
//            for (ScOutPortWarehouseGrossProfitSheet scOutPortWarehouseGrossProfitSheet : overList) {
//                if (sheet.getContractArea().equals(scOutPortWarehouseGrossProfitSheet.getContractArea())) {
//                    sheet.setOverSixAmount(scOutPortWarehouseGrossProfitSheet.getOverSixAmount());
//                    sheet.setOverSixWeight(scOutPortWarehouseGrossProfitSheet.getOverSixWeight());
//                    sheet.setOverSixPerKgFee(scOutPortWarehouseGrossProfitSheet.getOverSixPerKgFee());
//                    sheet.setOverSixParcelCount(scOutPortWarehouseGrossProfitSheet.getOverSixParcelCount());
//                    sheet.setOverSixPerParcelFee(scOutPortWarehouseGrossProfitSheet.getOverSixPerParcelFee());
//                    sheet.setOverSixProportion(BigDecimal.valueOf(scOutPortWarehouseGrossProfitSheet.getOverSixParcelCount() / (sheet.getOwParcelCount() + sheet.getIwParcelCount())));
//                }
//            }
            for (ScOutPortWarehouseGrossProfitSheet scOutPortWarehouseGrossProfitSheet : remote) {
                if (sheet.getName().equals(scOutPortWarehouseGrossProfitSheet.getName())) {
                    sheet.setRemoteAreaAmount(scOutPortWarehouseGrossProfitSheet.getRemoteAreaAmount());
                    sheet.setRemoteAreaPerKgFee(scOutPortWarehouseGrossProfitSheet.getRemoteAreaPerKgFee());
                    sheet.setRemoteAreaWeight(scOutPortWarehouseGrossProfitSheet.getRemoteAreaWeight());
                    sheet.setRemoteAreaParcelCount(scOutPortWarehouseGrossProfitSheet.getRemoteAreaParcelCount());
                    sheet.setRemoteAreaPerParcelFee(scOutPortWarehouseGrossProfitSheet.getRemoteAreaPerParcelFee());
                    if (sheet.getOwParcelCount() + sheet.getIwParcelCount() == 0){
                        sheet.setRemoteAreaProportion(new BigDecimal(0));
                    }else {
                        sheet.setRemoteAreaProportion(BigDecimal.valueOf(scOutPortWarehouseGrossProfitSheet.getRemoteAreaParcelCount() / (sheet.getOwParcelCount() + sheet.getIwParcelCount())));
                    }
                    sheet.setOverSixAmount(scOutPortWarehouseGrossProfitSheet.getOverSixAmount());
                    sheet.setOverSixWeight(scOutPortWarehouseGrossProfitSheet.getOverSixWeight());
                    sheet.setOverSixPerKgFee(scOutPortWarehouseGrossProfitSheet.getOverSixPerKgFee());
                    sheet.setOverSixParcelCount(scOutPortWarehouseGrossProfitSheet.getOverSixParcelCount());
                    sheet.setOverSixPerParcelFee(scOutPortWarehouseGrossProfitSheet.getOverSixPerParcelFee());
                    sheet.setOverSixProportion(BigDecimal.valueOf(scOutPortWarehouseGrossProfitSheet.getOverSixParcelCount() / (sheet.getOwParcelCount() + sheet.getIwParcelCount())));
                    sheet.setLnAmount(scOutPortWarehouseGrossProfitSheet.getLnAmount());
                    sheet.setLnWeight(scOutPortWarehouseGrossProfitSheet.getLnWeight());
                    sheet.setLnPerKgFee(scOutPortWarehouseGrossProfitSheet.getLnPerKgFee());
                    sheet.setLnParcelCount(scOutPortWarehouseGrossProfitSheet.getLnParcelCount());
                    sheet.setLnPerParcelFee(scOutPortWarehouseGrossProfitSheet.getLnPerParcelFee());
                    sheet.setLnProportion(BigDecimal.valueOf(scOutPortWarehouseGrossProfitSheet.getLnParcelCount() / (sheet.getOwParcelCount() + sheet.getIwParcelCount())));
                    if (sheet.getLnProportion() == null) {
                        sheet.setLnProportion(new BigDecimal(0));
                    }
                    if (sheet.getRemoteAreaProportion() == null) {
                        sheet.setRemoteAreaProportion(new BigDecimal(0));
                    }
                }
            }
//            for (ScOutPortWarehouseGrossProfitSheet scOutPortWarehouseGrossProfitSheet : ln) {
//                if (sheet.getContractArea().equals(scOutPortWarehouseGrossProfitSheet.getContractArea())) {
//                    sheet.setLnAmount(scOutPortWarehouseGrossProfitSheet.getLnAmount());
//                    sheet.setLnWeight(scOutPortWarehouseGrossProfitSheet.getLnWeight());
//                    sheet.setLnPerKgFee(scOutPortWarehouseGrossProfitSheet.getLnPerKgFee());
//                    sheet.setLnParcelCount(scOutPortWarehouseGrossProfitSheet.getLnParcelCount());
//                    sheet.setLnPerParcelFee(scOutPortWarehouseGrossProfitSheet.getLnPerParcelFee());
//                    sheet.setLnProportion(BigDecimal.valueOf(scOutPortWarehouseGrossProfitSheet.getLnParcelCount() / (sheet.getOwParcelCount() + sheet.getIwParcelCount())));
//                    if (sheet.getLnProportion() == null) {
//                        sheet.setLnProportion(new BigDecimal(0));
//                    }
//                }
//            }
            sheet.setTotalWeight(sheet.getIwWeight().add(sheet.getOwWeight()));
            sheet.setActualTransferCost(sheet.getTransferFee().subtract(sheet.getFixedReturn()));
            sheet.setReceivableWarehouseFee(scOutPortWareDispositionVo.getStorageCharge());
            sheet.setActualOWFee(sheet.getOwFee());
            sheet.setWarehouseFeeProfitAndLoss(scOutPortWareDispositionVo.getStorageCharge().subtract(sheet.getAccumulatedAmount()));
            sheet.setTransferFeeDeficit(sheet.getActualOWFee().subtract(sheet.getActualTransferCost()));
            sheet.setTotalProfitAndLoss(sheet.getTransferFeeDeficit().add(sheet.getWarehouseFeeProfitAndLoss()));
        }

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", PageInfo.info(sheets));
        jsonObject.put("fields", getTableVo());

        if (sheets == null || sheets.size() == 0) {
            return R.fail("暂无数据");
        } else {
            return R.ok(jsonObject);
        }
    }

    @Override
    public R updateDisposition(ScOutPortWareDispositionVo vo) {
        vo.setId("1");
        return scOutPortWarehouseGrossProfitSheetMapper.updateDisposition(vo) > 0 ? R.ok("修改成功") : R.fail("修改失败");
    }

    @Override
    public R selectDisposition() {
        return R.ok(scOutPortWarehouseGrossProfitSheetMapper.selectDisposition());
    }


    public List<TableVo> getTableVo() {

        //一级标题
        List<TableVo> first = new ArrayList<>();
        TableVo firstTableVo = new TableVo();firstTableVo.setFields("name");firstTableVo.setTranslate("承包区名称");first.add(firstTableVo);
        //应付总部金额
        firstTableVo = new TableVo();firstTableVo.setFields("headquarters");firstTableVo.setTranslate("应付总部金额");
        //应付总部金额-中转费金额
        List<TableVo> second = new ArrayList<>();
        TableVo secondTableVo = new TableVo();secondTableVo.setFields("transferFee");secondTableVo.setTranslate("中转费金额");second.add(secondTableVo);
        //应付总部金额-包干费返还
        secondTableVo = new TableVo();secondTableVo.setFields("fixedReturn");secondTableVo.setTranslate("包干费返还");second.add(secondTableVo);
        //应付总部金额-实际收取中转费
        secondTableVo = new TableVo();secondTableVo.setFields("actualCharges");secondTableVo.setTranslate("实际收取中转费");second.add(secondTableVo);
        //应付总部金额-包干费
        secondTableVo = new TableVo();secondTableVo.setFields("fix");secondTableVo.setTranslate("包干费");
        //应付总部金额-包干费-日包干费
        List<TableVo> third = new ArrayList<>();
        TableVo thirdTableVo = new TableVo();thirdTableVo.setTranslate("日包干费");thirdTableVo.setFields("fixedFee");third.add(thirdTableVo);
        //应付总部金额-包干费-累计金额
        thirdTableVo = new TableVo();thirdTableVo.setTranslate("累计金额");thirdTableVo.setFields("accumulatedAmount");third.add(thirdTableVo);
        secondTableVo.setSunList(third);second.add(secondTableVo);
        //应付总部金额-包仓内
        secondTableVo = new TableVo();secondTableVo.setFields("iw");secondTableVo.setTranslate("包仓内");
        //应付总部金额-包仓内-sunList
        third = new ArrayList<>();
        String[] iw = {"iwWeight", "iwStopLossWeight", "dailyAssessmentWeight", "iwDifference","iwFee","iwParcelCount","iwAverageWeight",
                "iwAssessmentAverageWeight","iwPerParcelFee","iwPerKgFee"};
        InitTableVo initTableVo = new InitTableVo();
        initTableVo.setFields(iw);
        initTableVo.setCode("scOutPortWare");
        third = tableService.initTable(initTableVo);
        secondTableVo.setSunList(third);
        second.add(secondTableVo);
        //应付总部金额-包仓外
        secondTableVo = new TableVo();secondTableVo.setFields("ow");secondTableVo.setTranslate("包仓外");
        //应付总部金额-包仓外-sunList
        third = new ArrayList<>();
        String[] ow = {"owWeight", "owFee", "owParcelCount", "owPerParcelFee","owPerKgFee"};
        initTableVo = new InitTableVo();
        initTableVo.setFields(ow);
        initTableVo.setCode("scOutPortWare");
        third = tableService.initTable(initTableVo);
        secondTableVo.setSunList(third);
        second.add(secondTableVo);
        //应付总部金额-6kg以上
        secondTableVo = new TableVo();secondTableVo.setFields("over");secondTableVo.setTranslate("包仓内6kg以上");
        //应付总部金额-6kg以上-sunList
        third = new ArrayList<>();
        String[] over = {"overSixParcelCount", "overSixWeight", "overSixAmount", "overSixPerParcelFee","overSixPerKgFee","overSixProportion"};
        initTableVo = new InitTableVo();
        initTableVo.setFields(over);
        initTableVo.setCode("scOutPortWare");
        third = tableService.initTable(initTableVo);
        secondTableVo.setSunList(third);
        second.add(secondTableVo);
        //应付总部金额-偏远
        secondTableVo = new TableVo();secondTableVo.setFields("remote");secondTableVo.setTranslate("偏远地区");
        //应付总部金额-偏远-sunList
        third = new ArrayList<>();
        String[] remote = {"remoteAreaParcelCount", "remoteAreaWeight", "remoteAreaAmount", "remoteAreaPerParcelFee","remoteAreaPerKgFee","remoteAreaProportion"};
        initTableVo = new InitTableVo();
        initTableVo.setFields(remote);
        initTableVo.setCode("scOutPortWare");
        third = tableService.initTable(initTableVo);
        secondTableVo.setSunList(third);
        second.add(secondTableVo);
        //应付总部金额-辽宁
        secondTableVo = new TableVo();secondTableVo.setFields("lN");secondTableVo.setTranslate("辽宁");
        //应付总部金额-辽宁-sunList
        third = new ArrayList<>();
        String[] lN = {"lnParcelCount", "lnWeight", "lnAmount", "lnPerParcelFee","lnPerKgFee","lnProportion"};
        initTableVo = new InitTableVo();
        initTableVo.setFields(lN);
        initTableVo.setCode("scOutPortWare");
        third = tableService.initTable(initTableVo);
        secondTableVo.setSunList(third);
        second.add(secondTableVo);
        //应付总部金额-总重量
        secondTableVo = new TableVo();secondTableVo.setFields("totalWeight");secondTableVo.setTranslate("总重量");second.add(secondTableVo);
        //应付总部金额-实际中转费
        secondTableVo = new TableVo();secondTableVo.setFields("actualTransferCost");secondTableVo.setTranslate("实际中转费");second.add(secondTableVo);
        firstTableVo.setSunList(second);
        first.add(firstTableVo);

        //应收承包区金额
        firstTableVo = new TableVo();firstTableVo.setFields("area");firstTableVo.setTranslate("应收承包区金额");
        second = new ArrayList<>();
        //应收承包区金额-包仓费
        secondTableVo = new TableVo();secondTableVo.setFields("receivableWarehouseFee");secondTableVo.setTranslate("包仓费");second.add(secondTableVo);
        //应收承包区金额-包仓外中转费
        secondTableVo = new TableVo();secondTableVo.setFields("actualOWFee");secondTableVo.setTranslate("包仓外中转费");second.add(secondTableVo);
        first.add(firstTableVo);

        //包仓中转毛利
        firstTableVo = new TableVo();firstTableVo.setFields("bc");firstTableVo.setTranslate("包仓中转毛利");
        second = new ArrayList<>();
        //包仓中转毛利-包仓费盈亏
        secondTableVo = new TableVo();secondTableVo.setFields("warehouseFeeProfitAndLoss");secondTableVo.setTranslate("包仓费盈亏");second.add(secondTableVo);
        //包仓中转毛利-中转费亏损
        secondTableVo = new TableVo();secondTableVo.setFields("transferFeeDeficit");secondTableVo.setTranslate("中转费亏损");second.add(secondTableVo);
        //包仓中转毛利-合计盈亏
        secondTableVo = new TableVo();secondTableVo.setFields("totalProfitAndLoss");secondTableVo.setTranslate("合计盈亏");second.add(secondTableVo);
        first.add(firstTableVo);

        return first;
    }
}
